<?php

namespace app\common\model;

use think\Model;

class Address extends Model
{
    protected $name = 'address';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    // 追加属性
    protected $append = [
    ];

    public function getList($user_id)
    {
        return self::all(compact('user_id'));
    }

    public function getAreaAttr($value, $data)
    {
        return [];
        /*return [
            'province' => Area::getNameById($data['province_id']),
            'city' => Area::getNameById($data['city_id']),
            'region' => Area::getNameById($data['region_id']),
        ];*/
    }

    public function add($use_id, $data)
    {
        if(isset($data['isdefault']) && $data['isdefault']){
            $listdata = $this->all(['user_id'=>$use_id]);
            foreach ($listdata as $key => $value) {
                $value['isdefault'] = '0';
                $value->save();
            }
        }

        // 添加收货地址
        /*$region = explode(',', $data['region']);
        $province_id = Area::getIdByName($region[0], 1);
        $city_id = Area::getIdByName($region[1], 2, $province_id);
        $region_id = Area::getIdByName($region[2], 3, $city_id);*/
        $this->allowField(true)->save(array_merge([
            'user_id' => $use_id,
            /*'province_id' => $province_id,
            'city_id' => $city_id,
            'region_id' => $region_id,*/
            'isdefault' => isset($data['isdefault']) && $data['isdefault'] ? 1 : 0,
        ], $data));

        return true;
    }

    public function setdefault($use_id,$id){
        $listdata = $this->all(['user_id'=>$use_id]);
        foreach ($listdata as $key => $value) {
            $value['isdefault'] = '0';
            $value->save();
        }
        self::where(['id'=>['<>',$id],'user_id'=>['=',$use_id]])->update(['isdefault'=>0]);
        return ($this->get($id))->save(['isdefault' => '1']);
    }

    public function del($id){
        return ($this->get($id))->delete();
    }

    public function detail($user_id, $id)
    {
        return self::get(compact('user_id', 'id'));
    }

    public function edit($data){
        /*$region = explode(',', $data['region']);
        $province_id = Area::getIdByName($region[0], 1);
        $city_id = Area::getIdByName($region[1], 2, $province_id);
        $region_id = Area::getIdByName($region[2], 3, $city_id);*/
        if($data['isdefault'] == 1){
            self::where(['id'=>['<>',$data['id']],'user_id'=>['=',$this['user_id']]])->update(['isdefault'=>0]);
        }
        return $this->allowField(true)
            ->save($data);
    }

    public static function getdefault($use_id){
        $filter = [];
        $filter['isdefault'] = '1';
        $filter['user_id'] = $use_id;
        return self::get($filter);
    }
}